#序列数据结构
#1、列表
list1=['中国','美国',1997,2001]
list2=[1,2,3,4,5]
list3=["a","b","c","d","e"]
#访问列表中的值
print("list1[0]:",list1[0])
print("list2[1:3]:",list2[1:3])
print("list3[-1]:",list3[-1])
#修改列表中的值
list1[0]=100
list2[1:3]=[200,300]
list3[-1]="f"
print("list1:",list1)
print("list2:",list2)
print("list3:",list3)
#删除列表中的元素
del list1[0]
list2.remove(200)
list3.pop()
print("list1:",list1)
print("list2:",list2)
print("list3:",list3)
#添加元素到列表中
list1.append(6)
list2.extend([7,8,9])
list3.insert(2,"g")
print("list1:",list1)
print("list2:",list2)
print("list3:",list3)
#2、元组
tuple1=(1,2,3,4,5)
tuple2=("a","b","c","d","e")
#空元组
tuple3=()
#只有一个元素的元组
tuple4=(1,)
#访问元组中的值
print("tuple1[0]:",tuple1[0])
print("tuple2[1:3]:",tuple2[1:3])
#连接元组
tuple5=tuple1+tuple2
print("tuple5:",tuple5)
#不能修改元组中的值，因为元组是不可变的
#删除整个元组
del tuple1
#将元组转换为列表
list1=list(tuple1)
list2=list(tuple2)
print("list1:",list1)
print("list2:",list2)
#将列表转换为元组
tuple1=tuple(list1)
tuple2=tuple(list2)
print("tuple1:",tuple1)
print("tuple2:",tuple2)
#将列表转换为字符串
str1=" ".join(list1)
str2=",".join(list2)
print("str1:",str1)
print("str2:",str2)
#3、字符串
#定义三行六列的二维列表，打印出元素值
list4=[[1,2,3],[4,5,6],[7,8,9]]
for i in range(3):
    for j in range(3):
        print(list4[i][j],end="\t")
    print()
#列表生成式
list5=[i for i in range(1,11)]
print(list5)
list6=[i*i for i in range(1,11)]
print(list6)
list7=[i*j for i in range(1,3) for j in range(1,3)]
print(list7)
#字典：键值对的无序集合
dict1={"name":"Alice","age":20,"city":"Beijing"}
print(dict1["name"])
print(dict1.get("age"))
print(dict1.get("gender","unknown"))
dict1["age"]=25
print(dict1)
dict1.update({"gender":"female"})
print(dict1)
dict1.pop("city")
#访问字典中的值
for key in dict1:
    print(key,dict1[key])
#删除字典中的元素
del dict1["name"]
print(dict1)
#修改字典中的元素
dict1["age"]=30
print(dict1)
#清空字典
dict1.clear()
print(dict1)
#in判断键是否存在字典中
print("age" in dict1)
#获取字典中所有值
print(dict1.values())
#items()方法获取字典中所有键值对
print(dict1.items())
for key,value in dict1.items():
    print(key,value)
#集合
set1={1,2,3,4,5}
set2={4,5,6,7,8}
#交集
print(set1&set2)
#并集
print(set1|set2)
#差集
print(set1-set2)
#对称差集
print(set1^set2)
#集合推导式
set3={i for i in range(1,11) if i%2==0}
print(set3)
#集合运算
set4={1,2,3,4,5}
set5={4,5,6,7,8}
#交集
print(set4&set5)
#并集
print(set4|set5)
#差集
print(set4-set5)
#对称差集
print(set4^set5)
#集合推导式
set6={i for i in range(1,11) if i%2==0}
print(set6)
#空集合
set7=set()
print(set7)
#判断输入年份是否为闰年
year=int(input("请输入年份："))
if (year%4==0 and year%100!=0) or year%400==0:
    print(year,"是闰年")
else:
    print(year,"不是闰年")
#判断输入的字符串是否为回文字符串
string=input("请输入字符串：")
if string==string[::-1]:
    print(string,"是回文字符串")
else:
    print(string,"不是回文字符串")

#items方法
dict2={"name":"Alice","age":20,"city":"Beijing"}
for key,value in dict2.items():
    print(key,value)